package cn.com.pansky.action.homePage;

import java.util.ArrayList;
import java.util.List;

import net.vsame.url2sql.action.CommonAction;
import net.vsame.url2sql.helper.SqlHelper;
import net.vsame.url2sql.sql.Model;
import net.vsame.url2sql.utils.PostParam;
import cn.com.pansky.helper.PostHelper;
import cn.com.pansky.sdk.API;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 我要查询
 * 
 * @author 王润华
 *
 */
public class QueryAction extends CommonAction {
	private JSONArray arrays = new JSONArray();

	// 基础参保信息查询
	public void query1() {
		String insured = getParam("insured");
		JSONObject result = new JSONObject();
		if(Integer.parseInt(insured) == 1 ) {
			
			result = querys("QA20001", "insuredQueryController.json");
			boolean success = result.getBoolean("SUCCESS");
			if (success) {
				JSONObject json = result.getJSONObject("MSG");
				String CA051 = json.getString("AAC005");
				String sql2 = "select AAA103 from AA11 where AAA100 = 'AAC005' and AAA102 = ? and AAA111 = '1'";
				Model m = SqlHelper.queryOne(sql2, CA051);
				CA051 = m.getString("AAA103");
				json.remove("AAC005");
				json.put("AAC005", CA051);
				context.put("result", json);
			}
		} else if (Integer.parseInt(insured) == 0 ){
			datas.put("isPass", false);
			datas.put("info", "此人未参保登记!");
			context.put("result", datas);
		} else {
			result = querys("QAAE016", "insuredQueryController.json");
			boolean success = result.getBoolean("SUCCESS");
			if (success) {
				JSONObject json = result.getJSONObject("MSG");
				context.put("result", json.getJSONArray("AAE016"));
			} else {
				context.put("result", new JSONArray());
			}
		}
	}

	// 缴费情况查询
	public void query2() {
		JSONObject result = querys("QAC50", "paymentQueryController.json");
		resultProc(result);
	}

	// 待遇领取情况查询
	public void query3() {
		JSONObject result = querys("QE10000", "retireQueryController.json");
		boolean success = result.getBoolean("SUCCESS");
		if (success) {
			JSONArray array = result.getJSONObject("MSG").getJSONArray("DYHJ");
			List<JSONObject> list = new ArrayList<JSONObject>();
			for (int i = 0; i < array.size(); i++) {
				JSONObject json = new JSONObject();
				json.put("AAE019", array.getJSONObject(i).getString("AAE019"));
				json.put("AAE002", array.getJSONObject(i).getString("AAE002"));
				json.put("AAE013", array.getJSONObject(i).getString("AAE002")
						.substring(4, 6)
						+ "月发放金额");
				list.add(json);
			}
			context.put("result", list);
		} else {
			context.put("result", new JSONArray());
		}
	}

	// 待遇信息查询
	public void query4() {
		JSONObject result = querys("QAC60", "treatmentQueryController.json");
		boolean success = result.getBoolean("SUCCESS");
		if (success) {
			queryAAA036();
			JSONArray array = result.getJSONObject("MSG").getJSONArray("DYBZ");
			List<JSONObject> list = new ArrayList<JSONObject>();
			for (int i = 0; i < array.size(); i++) {
				String str = array.getJSONObject(i).getString("AAA036");
				String AAA037 = "";
				for (int j = 0; j < arrays.size(); j++) {
					if (str.equals(arrays.getJSONObject(j).getString("AAA036"))) {
						AAA037 = arrays.getJSONObject(j).getString("AAA037");
						break;
					}
				}

				JSONObject json = new JSONObject();
				json.put("AAA036", AAA037);
				json.put("AAE041", array.getJSONObject(i).getString("AAE041"));
				json.put("AAE019", array.getJSONObject(i).getString("AAE019"));
				list.add(json);
			}
			context.put("result", list);
		} else {
			context.put("result", new JSONArray());
		}
	}

	// 认证记录查询
	public void query5() {
		String CA001 = getParam("CA001");
		Model model = SqlHelper.queryOne(
				"select CA003,CA006 from RC03 where CA001 = ?", CA001);
		List<PostParam> params = new ArrayList<PostParam>();
		PostParam AAC002 = new PostParam("AAC002", "string",
				model.getString("CA006"));// 身份证号
		PostParam AAC003 = new PostParam("AAC003", "string",
				model.getString("CA003"));// 姓名
		params.add(AAC002);
		params.add(AAC003);
		JSONObject results = PostHelper.send("auth/authRecords.json", params);
		int code = results.getInteger("code");
		if (code == 0) {
			JSONArray result = results.getJSONArray("datas");
			context.put("result", result);
		} else {
			context.put("result", new JSONArray());
		}
	}

	// 我的账户查询
	public void query6() {
		JSONObject result = querys("QAC50", "accountQueryController.json");
		resultProc(result);
	}
	public void query7(){
		JSONObject result = querys("QAC50", "accountQueryController.json");
		boolean success = result.getBoolean("SUCCESS");
		if (success) {
			JSONArray array = result.getJSONObject("MSG").getJSONArray("GRZHMX");
			context.put("result", array);
		} else {
			context.put("result", new JSONObject());
		}

	}
	
	// 参保等级查询
	public void insureInfo() {
		String queryInfo = urlConfig.getVar("getCA006");
		Model model = SqlHelper.queryOne(queryInfo);
		if(model != null) {
			JSONObject param = new JSONObject();
			param.put("AAA121", "QNWA20001"); //参保登记查询
			param.put("AAC002", model.getString("CA006")); // 身份证号
			JSONObject json = API.sendController("queryNetWorkinsuredController.json", param);
			JSONObject json2 = (JSONObject) json.getJSONArray("MSG").get(0);
			String CA051 = json2.getString("AAC005");
			String sql2 = "select AAA103 from AA11 where AAA100 = 'AAC005' and AAA102 = ? and AAA111 = '1'";
			Model m = SqlHelper.queryOne(sql2, CA051);
			CA051 = m.getString("AAA103");
			json2.remove("AAC005");
			json2.put("AAC005", CA051);
			context.getDatas().put("isPass", json.getBooleanValue("SUCCESS"));
			context.getDatas().put("result", json2);
		}else{
			context.getDatas().put("isPass", false);
			context.getDatas().put("info", "联系人不存在");
		}
	}

	public JSONObject querys(String AAA121, String str) {
		String CA001 = getParam("CA001");
		Model model = SqlHelper.queryOne(
				"select CA003,CA006 from RC03 where CA001 = ?", CA001);
		String CA003 = model.getString("CA003");
		String CA006 = model.getString("CA006");
		JSONObject param = new JSONObject();
		param.put("AAA121", AAA121);
		param.put("AAC003", CA003);
		param.put("AAC002", CA006);
		JSONObject result = API.sendController(str, param);
		return result;
	}

	public void resultProc(JSONObject result) {
		boolean success = result.getBoolean("SUCCESS");
		if (success) {
			context.put("result", result.getJSONObject("MSG"));
		} else {
			context.put("result", new JSONObject());
		}
	}

	// 查询待遇项
	public void queryAAA036() {
		JSONObject param = new JSONObject();
		param.put("AAC002", "1122");
		param.put("AAA121", "QAA17");
		param.put("AAA027", "610202");
		JSONObject result = API.sendController("treatmentProController.json",
				param);
		arrays = result.getJSONArray("MSG");
	}
	
	
	public static void main(String[] args) {
		JSONObject param = new JSONObject();
		param.put("AAA121", "QAC50");
		param.put("AAC003", "张荣花");
		param.put("AAC002", "610302195611050521");
		JSONObject result = API.sendController("accountQueryController.json", param);
		JSONArray array = result.getJSONObject("MSG").getJSONArray("GRZHMX");
		System.out.println(array.toString());
	}
	
}
